package com.b2j.language;

import com.b2j.base.ErrorManager;
import com.b2j.base.Lexema;
import com.b2j.base.MainTools;
import com.b2j.base.ProcessData;
import com.b2j.base.Routine;
import com.b2j.base.Token;
import com.b2j.base.TokenGroup;

public class Function extends Routine {

	@Override
	public String generate(Lexema lexema) {
		
		String line = "";
		lexema = MainTools.tools.nextLexema();
		
		// If the next token is a language component, discard it
		if (lexema.group == TokenGroup.LANGUAGE_COMPONENT) {
			
			lexema = MainTools.tools.nextLexema();
			
		}
		
		if (lexema.tokenType == Token.ID) {
			
			String name = lexema.lexema;
			ProcessData p = MainTools.tools.getProcess(name);
			
			MainTools.tools.parseTo(Token.BEGIN);
			
			if (p != null) {

				// Process the code inside the block of PROCESS
				line = line + MainTools.tools.processCodeUntil(lexema, Token.END);
				
				p.code = line;
				
			}
		} else {
			ErrorManager.unexpected(lexema);
		}
		
		return line;
	}

}
